package com.huaruan.youdi.views.fragments;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.huaruan.youdi.R;
import com.huaruan.youdi.adapter.OrderAdapter;
import com.huaruan.youdi.eventbus.EventBus;
import com.huaruan.youdi.eventbus.EventNames;
import com.huaruan.youdi.http.okhttp.Constant;
import com.huaruan.youdi.http.okhttp.HttpCallback;
import com.huaruan.youdi.http.okhttp.HttpPageUtils;
import com.huaruan.youdi.models.OrderModel;
import com.huaruan.youdi.utils.DeviceUtils;
import com.huaruan.youdi.utils.UserUtils;
import com.huaruan.youdi.views.widget.easyrecyclerview.EasyRecyclerView;
import com.huaruan.youdi.views.widget.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.huaruan.youdi.views.widget.easyrecyclerview.decoration.SpaceDecoration;

import java.util.ArrayList;
import java.util.SortedMap;
import java.util.TreeMap;

import butterknife.BindView;


public class MainChildFragment extends BaseFragment implements EventBus.SubscriberChangeListener {
    @BindView(R.id.recyclerView)
    EasyRecyclerView recyclerView;
    private OrderAdapter adapter;
    private String type;

    HttpPageUtils httpPageUtils;

    SpaceDecoration itemDecoration;

    @Override
    public int IGetMultiSatesContentViewResId() {
        return R.layout.fragment_main_child;
    }

    @Override
    public int IGetContentViewResId() {
        return 0;
    }

    @Override
    public void IFindViews(final View view) {
        Bundle bundle = getArguments();
        type = bundle.getString("type");
        adapter = new OrderAdapter(IGetContext(), type);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(IGetContext(),
                LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        if (itemDecoration == null) {
            itemDecoration = new SpaceDecoration(DeviceUtils.dp2px(5));
            recyclerView.addItemDecoration(itemDecoration);
        }
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
            }
        });

        adapter.setMore(R.layout.loadmore, new RecyclerArrayAdapter.OnMoreListener() {
            @Override
            public void onMoreShow() {
                httpPageUtils.next();
            }

            @Override
            public void onMoreClick() {

            }
        });
    }


    @Override
    public void IInitData() {
    }

    @Override
    public void IRequest() {
        getNo();
    }

    private void getNo() {
        SortedMap<String, String> sortMap = new TreeMap<>();
        sortMap.put("userId", UserUtils.getUid());
        sortMap.put("type", type);
        httpPageUtils = new HttpPageUtils(IGetContext())
                .url(Constant.GET_NO)
                .params(sortMap)
                .modelClass(OrderModel.class)
                .pageSize(10)
                .callback(new HttpCallback() {
                    @Override
                    public void success(Object o) {
                        hideLoadingAndRetryView();
                        ArrayList<OrderModel> models = (ArrayList<OrderModel>) o;
                        if (httpPageUtils.isFirstPage()) {
                            adapter.clear();
                        }
                        adapter.addAll(models);
                    }

                    @Override
                    public void failure(int status, String errorMsg) {
                        super.failure(status, errorMsg);
                        hideLoadingAndRetryView();
                    }
                });
        httpPageUtils.async2();
    }

    @Override
    public void onSubscriberDataChanged(Object notificationName, Object notificateContent) {
        if (EventNames.REFRESH_MAIN == notificationName) {
            if (notificateContent != null) {
                showContentLoadingView();
                httpPageUtils.first();
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        EventBus.getInstance().subscribe(EventNames.REFRESH_MAIN, this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getInstance().unSubscribe(EventNames.REFRESH_MAIN, this);
    }

}